package com.ruoyi.web.wx.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wx.domain.PtScore;
import com.ruoyi.wx.domain.vo.PtScoreVO;
import com.ruoyi.wx.service.IPtScoreService;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author moxiu
 * @since 2021-11-13
 */
@RestController
@RequestMapping("/wx/ptScore")
public class PtScoreController {

	@Autowired
	private IPtScoreService iPtScoreService;

	@PostMapping("/scanCode")
	@ApiOperation("扫码打分")
	public AjaxResult scanCode(@Valid @RequestBody PtScoreVO ptScoreVO) {
		if (ptScoreVO.getDiscipline() > Constants.SY_DISCIPLINE || ptScoreVO.getDiscipline() < Constants.MIN || ptScoreVO.getOperate() > Constants.SY_OPERATE || ptScoreVO.getOperate() < Constants.SY_DISCIPLINE) {
			throw new CustomException("打分不在正常范围内");
		}
		iPtScoreService.scanCode(ptScoreVO);
		return AjaxResult.success();
	}

	@GetMapping("/getScore")
	@ApiOperation("或许当前登录学生的成绩表")
	public AjaxResult getScore(Long expId) {
		final PtScore one = iPtScoreService.getOne(Wrappers.lambdaQuery(PtScore.class)
			.eq(PtScore::getExpId, expId)
			.eq(PtScore::getStuId, SecurityUtils.getLoginUser().getUser().getUserId())
		);
		if (one == null) {
			return AjaxResult.error("实验卡不存在");
		}
		return AjaxResult.success(one);
	}


}
